<!DOCTYPE html>
<html>
<head>
    <title>定时任务管理</title>
    <#include "/common/import.ftl" >
    <script type="text/javascript" src="${ctx}/js/utiltools.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#timeTaskList').datagrid({
                title: '定时任务列表',
                url: '${ctx }/task/list!data.action',
                striped: true,
                collapsible: true,
                pagination: true,
                singleSelect: true,
                nowrap: true,//数据长度超出列宽时将会自动截取。
                rownumbers: true,//行号
                fitColumns: true,//自动使列适应表格宽度以防止出现水平滚动
                remoteSort: false,
                idField: 'taskKey',
                columns: [[
                    {field: 'className', title: '名称', width: 200, align: 'center', sortable: true},
                    {field: 'methodName', title: '方法', width: 160, align: 'center', sortable: true},
                    {
                        field: 'taskType', title: '类型', width: 100, align: 'center', sortable: true,
                        formatter: function (value) {
                            if (value == 'spring') {
                                return '<font color=blue>接口继承</font>';
                            }
                            else if (value == 'remote') {
                                return '<font color=blue>远程调用</font>';
                            }
                            else {
                                return '<font color=blue>自定义类</font>';
                            }
                        }
                    },
                    {
                        field: 'description',
                        title: '描述',
                        width: 250,
                        align: 'center',
                        sortable: true
                    },
                    {
                        field: 'hour', title: '执行时间', width: 200, align: 'center', sortable: true,
                        formatter: function (val, rec) {
                            return rec.hour + ":" + rec.minute + ":" + rec.second;
                        }
                    },
                    {
                        field: 'startTime',
                        title: '启止时点',
                        width: 120,
                        align: 'center',
                        sortable: true,
                        formatter: function (val, rec) {
                            return rec.startTime + "--" + rec.endTime;
                        }
                    },
                    {
                        field: 'openStart',
                        title: '状态',
                        width: 100,
                        align: 'center',
                        sortable: true,
                        formatter: function (value) {
                            if (value == true) {
                                return '<font color=block>开启</font>';
                            }
                            else {
                                return '<font color=red>未开启</font>';
                            }
                        }
                    },
                    {
                        field: 'taskKey', title: '操作', width: 60, align: 'center', sortable: true,
                        formatter: function (value, rowData) {
                            if (rowData.openStart == true) {
                                var startUrl = "${ctx}/task/stop.action?key=" + value;
                                return "<a href='javascript:void(0);' onclick=\"executeTask('" + startUrl + "')\">关闭</a>";
                            } else {
                                var startUrl = "${ctx}/task/start.action?key=" + value;
                                return "<a href='javascript:void(0);' onclick=\"executeTask('" + startUrl + "')\">开启</a>";
                            }
                        }
                    }
                ]],
                toolbar: [{
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        saveCate();
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        deleteCate();
                    }
                }, '-', {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        editCate();
                    }
                }, '-', {
                    text: '启动任务',
                    iconCls: 'icon-ok',
                    handler: function () {
                        executeTask('${ctx}/task/start.action');
                    }
                }, '-', {
                    text: '停止任务',
                    iconCls: 'icon-no',
                    handler: function () {
                        executeTask('${ctx}/task/stop.action');
                    }
                }
                ]
            });
        });

        function executeTask(url) {
            $.ajax({
                type: "post",
                url: url,
                dataType: "json",
                success: function (data) {
                    var result = data;//jQuery.parseJSON(data);
                    if (result.flag == 'T') {
                        $.messager.alert('结果', '操作成功', 'info', function () {
                            $('#timeTaskList').datagrid('reload');
                        });
                    } else {
                        $.messager.alert('结果', result.msg, 'error');
                    }
                }
            });
        }

        // 进入增加页面
        function saveCate() {
            $('#saveFrame').html('');
            var url = '${ctx}/task/save';
            $('#saveFrame').attr("title", '');
            $('#saveFrame').attr("src", url);
            $('#saveDiv').window('open');
        }

        // 点击取消按钮，关闭添加窗口
        function colseAdd() {
            $('#saveDiv').window('close');
        }

        // 刷新列表
        function cateReload() {
            $('#timeTaskList').datagrid('reload');
        }

        // 判断是否选中一条记录
        function getSelected() {
            var selected = $('#timeTaskList').datagrid('getSelected');
            if (selected) {
                return selected;
            } else {
                $.messager.alert('提示', '请选择要操作的数据', 'info');
            }
        }

        // 进入修改页面
        function editCate() {
            var node = getSelected();
            if (node) {
                var url = '${ctx}/task/save?key=' + node.taskKey;
                $('#saveFrame').attr("title", node.className);
                $('#saveFrame').attr("src", url);
                $('#saveDiv').window('open');
            }
        }

        //删除，物理删除
        function deleteCate() {
            var node = getSelected();
            if (node) {
                $.messager.confirm('确认', '您确定要删除:<font color=red>' + node.className + '</font> ？', function (r) {
                    if (r) {
                        $.ajax({
                            type: "post",
                            url: "${ctx}/task/del.action?key=" + node.taskKey,
                            dataType: "json",
                            success: function (data) {
                                var result = data;//jQuery.parseJSON(data);
                                if (result.flag == 'T') {
                                    $.messager.alert('结果', '操作成功', 'info');
                                    var rowIndex = $('#timeTaskList').datagrid('getRowIndex', node);
                                    $('#timeTaskList').datagrid('deleteRow', rowIndex);
                                } else if (result.flag == 'F') {
                                    $.messager.alert('结果', result.msg, 'info');
                                } else {
                                    $.messager.alert('结果', '操作失败，请重试', 'error');
                                }
                            },
                            error: function (messg) {
                                $.messager.alert('错误提示', '操作失败:' + messg.responseText, 'error');
                            }
                        });
                    }
                });
            }
        }

    </script>
</head>
<body>
<table id="timeTaskList"></table>

<!-- 添加窗口 -->
<div id="saveDiv" class="easyui-window" title="定时任务定义"
     style="padding:5px;width: 580px; height:350px; overflow: hidden;"
     iconCls="icon-search" closed="true" maximizable="false" minimizable="false"
     collapsible="false">
    <iframe frameborder="0" id="saveFrame" height="100%" width="99%" scrolling="yes"
            frameborder="0"></iframe>
</div>
</body>
</html>